<template>
	
	<view class="home">
		<van-popup :show="tipshow" @close="tipshow=false">
			<view class="tishi">
				<view class="tstou">提示</view>
				<view class="tsnei">
					请先扫设备二维码绑定小区后再操作。
				</view>
				<view class="kbtn" @click="toScan">
					确认
				</view>
			</view>
		</van-popup>
	</view>

</template>

<script>
	import {
		Config
	} from '../../common/Config.js'

	export default {
		data() {
			return {
				tipshow: false,
				code: '',
				deviceId: '', //设备号
				port: '', //端口号
				appKey: '', //公众号
			}
		},
		onLoad(options) {
			let that = this;
			if(options.deviceId && options.port){
				console.log(options)
				that.deviceId = options.deviceId;
				that.port = options.port;
				if(uni.getStorageSync('userToken') == ''){
					that.wxlogin();
				}
				else{
					uni.redirectTo({
						url: '/pages/poweron/poweron?device=' + that.deviceId + '&port=' + that.port +
							'&cmd=' + 0 + '&from=' + 1,
					});
				}
			}
			else{
				that.appKey = Config.appkey;
				if(uni.getStorageSync('userToken') == ''){
					that.wxOfficalLogin();
				}
				else{
					uni.redirectTo({
						url: '/pages/smcharging/smcharging',
					});
				}
			}
		},
		methods: {
			//扫端口码登录
			wxlogin(){
				let that = this;
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event){
						console.log(event);
						const code = event.code;
						let data = {
							deviceId: that.deviceId,
							port: that.port,
							appkey: Config.appkey,
							code: code
						}
						that.$base.request('wxlogin/wxapp/getcode', 'POST', data)
							.then(res => {
								let ss = '';
								if (that.port != 'ff' && that.port != 'FF') {
									ss = that.pad(parseInt(that.port, 16).toString(10), 2);
								} else {
									ss = that.port;
								};
								if (res.data.code == '200') {
									uni.setStorageSync('user', res.data.data);
									uni.setStorageSync('userToken', res.data.data.token);
									uni.setStorageSync('userId', res.data.data.id);
									uni.redirectTo({
										url: '/pages/poweron/poweron?device=' + that.deviceId + '&port=' + ss +
											'&cmd=' + 0 + '&from=' + 1,
									});
									
								} else {
									var applet_openid = res.data.data.applet_openid;
									var appid = res.data.data.appid;
									var secret = res.data.data.secret;
									uni.redirectTo({
										url: '/pages/offical_login/offical_login?device=' + that.deviceId + '&port=' + ss +
											'&applet_openid=' + applet_openid + '&appid=' + appid + '&secret=' + secret,
									});
								}
							})
							.catch(err => {})
					},
					fail: function (err) {
				        // 登录授权失败
				        // err.code是错误码
				    }
				})
			},
			
			//公众号菜单登录
			wxOfficalLogin(){
				let that = this;
				uni.login({
					"provider": "weixin",
					"onlyAuthorize": true, // 微信登录仅请求授权认证
					success: function(event){
						console.log(event);
						const code = event.code;
						let data = {
							appKey: that.appKey,
							code: code
						}
						that.$base.request('wxlogin/wxapp/getcode', 'POST', data)
							.then(res => {
								let ss = '';
								if (that.port != 'ff' && that.port != 'FF') {
									ss = that.pad(parseInt(that.port, 16).toString(10), 2);
								} else {
									ss = that.port;
								};
								if (res.data.code == '200') {
									uni.setStorageSync('user', res.data.data)
									uni.setStorageSync('userId', res.data.data.id);
									uni.setStorageSync('userToken', res.data.data.token)
									uni.redirectTo({
										url: '/pages/smcharging/smcharging',
									});
								} else {
									var applet_openid = res.data.data.applet_openid;
									var appid = res.data.data.appid;
									var secret = res.data.data.secret;
									uni.redirectTo({
										url: '/pages/offical_login/offical_login?appKey=' + that.appKey +
											'&applet_openid=' + applet_openid + '&appid=' + appid + '&secret=' + secret,
									});
								}
							})
							.catch(err => {})
					},
					fail: function (err) {
				        // 登录授权失败
				        // err.code是错误码
				    }
				})
			},
			//格式化16进制
			pad(num, n) {
				var len = num.toString().length;
				while (len < n) {
					num = "0" + num;
					len++;
				}
				return num;
			},
			//弹出扫码
			toScan() {
				uni.scanCode({
					scanType: ['qrCode'],
					success: function (res) {
						console.log('条码类型：' + res.scanType);
						console.log('条码内容：' + res.result);
						var str = res.result;
						var str_arr = str.split('/'+Config.appkey+'/');
						var params_arr = str_arr[1].split('/');
						var deviceId = params_arr[0];
						var port = params_arr[1];
						uni.navigateTo({
							url: '/pages/poweron/poweron?device=' + deviceId + '&port=' + port +
								'&cmd=' + 0 + '&from=' + 1,
						});
					}
				});
			},
			
		}
	}
</script>

<style>
	.home {
		width: 100%;
	}
</style>